package com.dolphin.crossbrowser;

import android.net.http.SslError;
import android.util.Log;
import android.webkit.ValueCallback;
import android.webkit.WebResourceResponse;

import org.xwalk.core.ClientCertRequest;
import org.xwalk.core.XWalkHttpAuthHandler;
import org.xwalk.core.XWalkResourceClient;
import org.xwalk.core.XWalkView;
import org.xwalk.core.XWalkWebResourceRequest;
import org.xwalk.core.XWalkWebResourceResponse;

import java.io.InputStream;
import java.util.Map;

public class XWalkResourceClientImpl extends XWalkResourceClient {
    public static final String TAG = "XWalkResourceClientImpl";

    XWalkResourceClientImpl(XWalkView view) {
        super(view);
    }

    @Override
    public void onDocumentLoadedInFrame(XWalkView view, long frameId) {
        Log.d(TAG, "onDocumentLoadedInFrame frameId:" + frameId);
        super.onDocumentLoadedInFrame(view, frameId);
    }

    @Override
    public void onLoadStarted(XWalkView view, String url) {
        Log.d(TAG, "onLoadStarted url:" + url);
        super.onLoadStarted(view, url);
    }

    @Override
    public void onLoadFinished(XWalkView view, String url) {
        Log.d(TAG, "onLoadFinished url:" + url);
        super.onLoadFinished(view, url);
    }

    @Override
    public void onProgressChanged(XWalkView view, int progressInPercent) {
        Log.d(TAG, "onProgressChanged " + progressInPercent);
        super.onProgressChanged(view, progressInPercent);
    }

    @Override
    public WebResourceResponse shouldInterceptLoadRequest(XWalkView view, String url) {
        //Log.d(TAG, "shouldInterceptLoadRequest url:" + url);
        return super.shouldInterceptLoadRequest(view, url);
    }

    @Override
    public XWalkWebResourceResponse shouldInterceptLoadRequest(XWalkView view, XWalkWebResourceRequest request) {
        //Log.d(TAG, "shouldInterceptLoadRequest request:" + request.getUrl());
        return super.shouldInterceptLoadRequest(view, request);
    }

    @Override
    public void onReceivedLoadError(XWalkView view, int errorCode, String description, String failingUrl) {
        Log.d(TAG, "onReceivedLoadError errorCode:" + errorCode + " description:" + description + " failingUrl:" + failingUrl);
        super.onReceivedLoadError(view, errorCode, description, failingUrl);
    }

    @Override
    public boolean shouldOverrideUrlLoading(XWalkView view, String url) {
        Log.d(TAG, "shouldOverrideUrlLoading url:" + url);
        return super.shouldOverrideUrlLoading(view, url);
    }

    @Override
    public void onReceivedSslError(XWalkView view, ValueCallback<Boolean> callback, SslError error) {
        Log.d(TAG, "onReceivedSslError error:" + error.getUrl());
        super.onReceivedSslError(view, callback, error);
    }

    @Override
    public void onReceivedClientCertRequest(XWalkView view, ClientCertRequest handler) {
        Log.d(TAG, "onReceivedClientCertRequest");
        super.onReceivedClientCertRequest(view, handler);
    }

//    @Override
//    public void onReceivedResponseHeaders(XWalkView view, XWalkWebResourceRequest request, XWalkWebResourceResponse response) {
//        Log.d(TAG, "onReceivedResponseHeaders");
//        super.onReceivedResponseHeaders(view, request, response);
//    }

    @Override
    public void doUpdateVisitedHistory(XWalkView view, String url, boolean isReload) {
        Log.d(TAG, "doUpdateVisitedHistory url:" + url);
        super.doUpdateVisitedHistory(view, url, isReload);
    }

    @Override
    public void onReceivedHttpAuthRequest(XWalkView view, XWalkHttpAuthHandler handler, String host, String realm) {
        Log.d(TAG, "onReceivedHttpAuthRequest host:" + host);
        super.onReceivedHttpAuthRequest(view, handler, host, realm);
    }

    @Override
    public XWalkWebResourceResponse createXWalkWebResourceResponse(String mimeType, String encoding, InputStream data) {
        Log.d(TAG, "createXWalkWebResourceResponse 1");
        return super.createXWalkWebResourceResponse(mimeType, encoding, data);
    }

    @Override
    public XWalkWebResourceResponse createXWalkWebResourceResponse(String mimeType, String encoding, InputStream data, int statusCode, String reasonPhrase, Map<String, String> responseHeaders) {
        Log.d(TAG, "createXWalkWebResourceResponse 2");
        return super.createXWalkWebResourceResponse(mimeType, encoding, data, statusCode, reasonPhrase, responseHeaders);
    }
}
